<template>
  <BaseDialog
    ref="baseDialog"
    :title="isEdit ? '编辑' : '新增'"
    @confrim="confrim"
    @cancel="close"
  >
    <BaseForm
      labelWidth="150px"
      :fromIems="fromIems"
      :size="'medium'"
      ref="BaseForm"
      @submit="submit"
      @edit="edit"
    ></BaseForm>
  </BaseDialog>
</template>

<script>
import { Message } from "element-ui";
import { getId, addBanner, editBanner } from "@/api/common";
import { rsaPassword, uploadImg, searchFormParams } from "@/utils";
export default {
  name: "add",
  data() {
    return {
      title: "",
      fromIems: [
        {
          valueType: "img",
          label: "轮播图片",
          dataIndex: "imgUrl",
          uplaod: (e) => this.handleUpload(e),
          rules: [{ required: true, message: "请上传", trigger: "blur" }],
          defaultValue: "",
        },
        {
          valueType: "select",
          label: "显示位置",
          dataIndex: "which",
          options: [
            {
              label: "都显示",
              value: 3,
            },
            {
              label: "显示在小程序",
              value: 1,
            },
            {
              label: "显示在网页",
              value: 2,
            },
          ],
          rules: [{ required: true, message: "请选择", trigger: "blur" }],
        },
        {
          valueType: "select",
          label: "状态",
          dataIndex: "state",
          hide:(data)=>!data.id,
          options: [
            {
              label: "上架",
              value: 1,
            },
            {
              label: "下架",
              value: 0,
            },
           
          ],
          rules: [{ required: true, message: "请选择", trigger: "blur" }],
        },
        {
          valueType: "inputNumber",
          label: "排序",
          dataIndex: "sort",
          rules: [{ required: true, message: "请输入", trigger: "blur" }],
        },
        {
          valueType: "select",
          label: "链接功能",
          dataIndex: "linkUrl",
          options: [
            {
              label: "共享",
              value: 1,
            },
            {
              label: "二手",
              value: 2,
            },
            {
              label: "配件",
              value: 3,
            },
            {
              label: "以旧换新",
              value: 4,
            },
            {
              label: "入驻",
              value: 5,
            },
            {
              label: "官网",
              value: 6,
            },
          ],
        },
        {
          valueType: "input",
          label: "备注说明",
          dataIndex: "note",
          type: "textarea",
        },
      ],
      isEdit: false,
    };
  },
  methods: {
    open(row) {
      row ? (this.isEdit = true) : (this.isEdit = false);
      this.$refs.baseDialog.open();
      this.$nextTick(() => {
        if (row) {
          for (var k in row) {
            this.$set(this.$refs.BaseForm.form, k, row[k]);
            searchFormParams(this, "imgUrl").defaultValue = row["imgUrl"];
          }
        } else {
          this.$refs.BaseForm.form = {};
        }
      });
    },

    handleUpload(e) {
      uploadImg(e, 1024 * 1024, {}, "banner", (res) => {
        searchFormParams(this, "imgUrl").defaultValue = res;
      });
    },
    close() {
      searchFormParams(this, "imgUrl").defaultValue = null;
      this.$refs.baseDialog.close();
      this.$refs.BaseForm.rest();
    },
    confrim() {
      if (this.isEdit) {
        this.$refs.BaseForm.edit();
      } else {
        this.$refs.BaseForm.submit();
      }
    },
    async submit(data) {
      let params = { ...data };
      params.state = 0;
      const res = await addBanner(params);
      if (!res.status) {
        this.$emit("success");
        Message.success("success");
        this.close();
      }
    },
    async edit(data) {
      const res = await editBanner({
        id: data.id,
        imgUrl: data.imgUrl,
        which: data.which,
        linkUrl: data.linkUrl,
        sort: data.sort,
        note: data.note,
        state:data.state
      });
      if (!res.status) {
        this.$emit("success");
        Message.success("success");
        this.close();
      }
    },
  },
};
</script>

<style lang="scss" scoped></style>
